<?php
$isfinded=0;
$searray=array();
$qian2=array();
$newarray=array();
$smdata=array();
$qian=array();
$data0=array();
$isfinded=false;
$arr=array();
$filter="";
$filters="";
$filterLine="44335";

/**
 * @param $data
 * @param $key
 * @param $q
 * @return mixed
 * 代号化数组
 */
function bl($data,$key="",$q="")
{
    if(!is_string($key)){
        $q = ($key === null) ? "\$data" : $q . "[{$key}]";
    }else {
        $q = ($key === null) ? "\$data" : $q . "[\"{$key}\"]";
    }
    if(!empty($data)) {
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $q = str_replace("[\"\"]", "", $q);
                $data[$k] = bl($v, $k, $q);//代号代数组
            } else {
                if (!is_string($k)) {
                    $data[$k] = "{$q}[{$k}]";
                } elseif ($q == "[\"\"]") {
                    $data[$k] = "[\"{$k}\"]";
                } else {
                    $data[$k] = "{$q}[\"{$k}\"]";
                }
            }
        }
    }
    return $data;
}

function bl0($data,$key="data")
{
    if(!empty($data)) {
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $data[$k] = bl0($v, $k);
            } else {
                if (!is_string($k)) {
                    $data[$k] = $v;
                } elseif ($k == null) {
                    $data[] = preg_replace("/\r|\n/", " ", $data[$k]);
                } else {
                    $data[$k] = preg_replace("/\r|\n/", " ", $data[$k]);
                }
            }
        }
    }
    return $data;
}


function createCode($data,$key="",$q="")
{
    global $newarray,$smdata,$filters,$temparr,$searray,$filterLine,$temparr;
    static $count=0,$aa=0;
    $count++;
    if($count==3){
        $a=1;
    }
    bugv();
    if(!is_string($key)){
        $q = ($key === null) ? "\$data" : $q . "[{$key}]";
    }else {
        $q = ($key === null) ? "\$data" : $q . "[\"{$key}\"]";
    }

    if(!empty($data)) {
        foreach ($data as $k => $arrKeyArr) {
            if (!is_string($k)) {
                $data[$k] = "{$q}[{$k}]";
            } elseif ($q == "[\"\"]") {
                $data[$k] = "[\"{$k}\"]";
            } else {
                $data[$k] = "{$q}[\"{$k}\"]";
            }
            $re=addsla4($data[$k]);
            $data[$k] = preg_replace("/^\[\"\"\]/", "", $data[$k]);
            $currPath=$data[$k];
            $isMatched=false;
            $isMatchFilter = preg_match("/^(.*{$re})(?:\:|：)([^\r\n\,]*)(?:\,([^\,\r\n\[\]]+))?(?:\,\[([^\r\n]*)\]\s*)?.*$/imx", $filters, $filternn);
            if($isMatchFilter) {
                $re2=addsla4($filternn[1]);
                if(preg_match("/{$re2}/Uisx",$currPath)){
                    $GLOBALS['var']['currFilterLine']=$filternn[0];
                    $isMatched=true;
                }
            }

            if($isMatched) {
                $filter=$filternn;
                $filterLine=$filternn[0];  //如果路径配置某一条filter
                $name = $filternn[2];
                $filternn[3]=preg_replace("/\(\?.*\)/","",$filternn[3]);
                //如果在filter中找到含有当前代号的filter，分析这一个filter的各个参数
                if (preg_match("/\[\"{$filternn[3]}\"\](\[([0-9]+)\])?/", $currPath, $ii)) {
                    $arrKey = $filternn[3];   //号后面的第二个参数是要获得数据的名称
                    $arrIndex=$ii[1];   //filter的数组的序号，一般为0
                } else {
                    $arrKey = "";
                }
            }else{
                $filter="";
                $filterLine="";
                $name="";
            }

            if(preg_match("/skuWeight/i",$currPath)){
                $debugger=true;  //常用断点
                $is=$filterLine;
            }

            //赋值入口_分数组或字符
            if (is_array($arrKeyArr)) {
                    $info=[$name];
                    //如果值返回的是数组形式
                    $q = str_replace("[\"\"]", "", $q);
                    if ($isMatched) {
                        if($debugger){
                            $a=1;
                        }
                        $aa++;
                        array_merges($currPath, $name, $arrIndex, $filter);//新数据、路径、新名字、index,命令拆分,最后值
                    } else {
                        //不匹配,则继续循环
                        $data[$k] = createCode($arrKeyArr, $k, $q);
                    }
            } else {
                    if ($isMatched&&$currPath) {
                        //如果值是字符型、数字型、布尔型，直接合并到【最终新数组】
                        $newkey = $filter[1];
                        if($debugger){
                            $a=2;
                        }
                        array_merges($data[$k], $newkey, $arrIndex, $filter);
                    }
                }
            }

    }
    return $data;
}

/**
 * @param $array
 * @param $re
 * @return array|mixed
 * 分析$array每一项是否匹配路径规则,符合的就收录到$searray中
 */
function AnalyzeSeArray($re){
    $nn=explode(",",$re);
    $r=array(); //收集要查找的name值,以及要重新命名的key
    if($re!="") {
        for ($i = 0; $i < count($nn); $i++) {
            $name_tmp = explode(":", $nn[$i]);
            $namestr = $name_tmp[0];
            $nameNew = $name_tmp[1];
            $nameass=preg_split("/\|/", $namestr);
            $r[] = array("nameStr" => $namestr,"nameNew" => $nameNew,'nameAsS'=>$nameass);
        }
    }
    return $r;
}

/**
 * @param $newarray
 * @param $strings
 * @param $name
 * @param $index
 * @param $prenn
 * @return mixed
 * 判断结构$string,将对应$data0，合并到数组$newarray，以及 $smdata中
 */
function array_merges($currPath,$name=null,$index,$prenn=array()){
    global $data0,$qian,$smdata,$r,$mustInArr,$k,$arrSelectKeys,$temparr,$qian22_pre,$temparr2,$temparr3;

    preg_match_all("/\[([^\[\]]*)\]/Uisx",$currPath,$nn);
    $arrTiaojian=false;
    if(preg_match("/([^\[\],]*)==\"([^\[\],]*)\"/Uisx",$prenn[0],$nn55)) {
        $arrTiaojian=true;
    }
    $name=$prenn[2];
    $arrName=$prenn[3];
    $arrSelectKeys=$prenn[4];
    $r = AnalyzeSeArray($arrSelectKeys);
    $mustInArr=mustInArr($r);
    $temparr3=array();
    for($i=0;$i<count($nn[1]);$i++){
        if($i<count($nn[1])-1) {
        }else {
            //到了最后一个时：
            $currPathNew=preg_replace("/\[[^\[\]]*\](?=$)/","[\"{$name}\"]",$currPath);//将最后元素名字换成要替换的名称
            if($index!="") {
                if(preg_match("/(\[\"{$arrName}\"\])\[[0-9]+\]/Uisx",$currPath)) {
                    $cstring = preg_replace_callback("/^(.*\[\"{$arrName}\"\])\[[0-9]+\].*$/Uisx", function ($em) {
                        return $em[1];
                    }, $currPath);
                    $arrNameCatchArr= eval("return \$data0" . $cstring . ";");
                    $searray=array();
                    $temparr=array();
                    $temparr3=array();
                    foreach ($arrNameCatchArr as $j=>$v) {
                        if($arrTiaojian&&$arrNameCatchArr[$j][$nn55[1]]!=$nn55[2]){
                            continue;
                        }else{
                            if ($arrName == "") {
                                eval("\$smdata[\$arrName][$name]= \$data0" . $currPath . ";");
                            } else {
                                $currPath = preg_replace_callback("/(\[\"{$arrName}\"\]\[)[0-9]+(\])/Uisx", function ($em) use ($j) {
                                    return $em[1] . $j . $em[2];
                                }, $currPath);
                                $currPathNew = preg_replace_callback("/(\[\"{$arrName}\"\]\[)[0-9]+(\])/Uisx", function ($em) use ($j) {
                                    return $em[1] . $j . $em[2];
                                }, $currPathNew);
                                $value = eval("return \$data0" . $currPath . ";");
                                $searray=array();
                                if (isNumericKeyArray($value)) {
                                  //--yzs捕获数组的第一重循环1$k
                                  for ($k = 0; $k < count($value); $k++) {
                                        $qian=array();
                                        $qian[]=$k;
                                        $searray[$k]=findKeyInArr($value[$k],$currPath,$qian,0);
                                    }
                                } else {
                                    $searray[]=findKeyInArr($value,$currPath,$qian,0);
                                }
                                $joinStr=joinStr($searray,$prenn);
                                @eval("\$smdata['{$name}']= \$joinStr;");
                            }
                        }
                    }
                    $a=1;
                }else{
                    $value = eval("return \$data0" . $currPath . ";");
                }
            }else{
                $ni=new NameInfo();
                $arrName=$ni->clname($arrName);
                $searray=array();
                $temparr=array();
                $value=$ni->getData($data0,$currPath);
                if (isNumericKeyArray($value)) {
                    //--yzs捕获数组的第一重循环2$k
                    for ($k = 0; $k < count($value); $k++) {
                        $qian=array();
                        $qian[]=$k;
                        $temparr2=array();
                        $ni->getData($data0,$currPath,$k);
                        $searray[$k]=findKeyInArr($value[$k],$currPath,$qian,0,$ni);
                        if($qian22_pre){
                            $qian22_pre=$ni->nameValue??$qian22_pre;
                            $searray[$k][$qian22_pre]=$temparr2;
                            $qian22_pre="";
                            $temparr2=array();
                        }
                    }
                } else {
                    $searray[]=findKeyInArr($value,$currPath,$qian,0);
                }
                if(preg_match("/\,easy(\,|$)/i",$GLOBALS['var']['currFilterLine'])){
                    $searray=easy($searray);
                }
                $joinStr=joinStr($searray,$prenn);
                if(is_array($joinStr)&&count($joinStr)==1){
                    $joinStr=current($joinStr);
                }
                eval("\$smdata['$name']= \$joinStr;");
            }

        }
    }

}


/**
 * @param $r 要从数组中查找的name值和新名key
 * @param $array 数组  --yzs 捕获数组
 * @param $qian 前面走过的key的路径数组，$qian2是临时的$qian
 * @return $searray 收集好的数组
 * 寻找key值是否存在该数组
 */
function findKeyInArr($array,$currPath,$qian=array(),$level=0,$ni=null){
    global $r,$mustInArr,$k,$k2,$name,$temparr,$temparr2,$temparr3,$nameAsS_paths_verif,$qian22,$qian22_pre;
    if(empty($r)){
        $searray=$array;
        return $array;
    }
    $searray=array();
    //--yzs捕获数组的第二重循环$k2
    foreach($array as $k2=>$v){
        //从数组值中遍历每个值
        $k2_continue=false;
        if(empty($qian)){
            $qian2=array();
        }else{
            $qian2=$qian;
        }

        if(is_array($v)){
            //如果值是数组，需要全部遍历子数组
            //收集数组的k值
            $qian2[]=$k2;
            $array[$k2]=findKeyInArr($v,$currPath,$qian2,$level+1);//$qian2是收集key路径的数组，做为下一次收集的qian参数
        }else{
            $qian2[]=$k2;
            //如果值是非数组的
            //从数组值中遍历每个值中，收集count($r)个元素生成一个$temparr
            //--yzs捕获数组的第三重循环$r $r是要收集的数组名称
            for($j=0;$j<count($r);$j++) {
                //遍历需要搜集的$r数组名字,共count($r)个
                if($k2_continue) break;

                $nameNew = $r[$j]['nameNew'];
                $nameAsS=$r[$j]['nameAsS'];

                //--yzs捕获数组的第四重循环|
                for($i=0;$i<count($nameAsS);$i++){
                    //如果有二级多个名字（用|隔开的）
                    $nameAsS_paths=explode(">",$nameAsS[$i]);
                    //如果带有判断前路径集
                    $name=end($nameAsS_paths); //路径集最后一个元素是旧名字
                    $nameNew=$nameNew==""?$name:$nameNew;
                    if(is_array($nameAsS_paths)&&count($nameAsS_paths)>0){
                        //如果存在>号,即需要判断是哪个路径下的
                        $nameAsS_paths_verif=array_ecptnum($nameAsS_paths);//去除数字序号后的要求验证顺序
                        $qian22=array_ecptnum($qian2);//去除数字序号后的实际验证顺序
                        $ub=prints("nameAsS_paths_verif,qian22");
                        if($nameAsS_paths_verif==$qian22||!is_int($k2)&&$k2==end($nameAsS_paths_verif)){
                            if ($k2==end($nameAsS_paths_verif)) {
                                $qian22_pre=$qian22[count($qian22)-2];
                                if($qian22_pre){
                                    $temparr3[$nameNew]=$v;
                                }else {
                                    $temparr[$nameNew] = $v;
                                }
                                break;
                            }
                        }
                    }else{
                        //如果不存在>号,即不需要判断是哪个路径下的
                        $str=prints("k,k2,mustInArr");
                        if (in_array($k2,$mustInArr)) {
                            //如果当前数组单元的k,存在于需要收集的集合中
                            $temparr[$nameNew] = $v; //$index是当前子数组的index
                            $k2_continue = true;
                            break;
                        }
                    }
                }
            }
            if($k2_continue) continue;
        }
    }
    if($level==2){
        $temparr2[]=$temparr3;
    }
    return $temparr;
}

function array_ecptnum($arr){
    $result=array();
    foreach($arr as $k=>$v){
        if(!preg_match("/^[0-9]+$/",$v)){
            $result[]=$v;
        }
    }
    return $result;
}

function lianjie($newr,$s){
    global $qian;
    $ss="";
    foreach($qian as $k=>$v){
        $ss.="[$v]";
    }
    $test= eval("return \$newr" . $ss . "[$s];");
    if (!isset($test)) {
        eval("return \$newr" . $ss . "[$s]=array();");
    }
    $qian[]=$s;
    return $newr;
}

function save(){
    global $arr;
    $data=array();
    $host=host($_REQUEST['url']);
    if($_REQUEST['likere']==""){
        $s=preg_replace("/https?\:\/\/[^\/]*(.*\/([^\/\?]+)).*$/isx","\\1",$_REQUEST['url']);
        $s=preg_replace("/(\/|\?|\:|\.|\-)/","\\\\\\1",$s);
        $_REQUEST['likere']=$s;
    }
    $data['host']=$host;
    $data['url']=$_REQUEST['url'];
    $data['postdata']=$_REQUEST['postdata'];
    $data['json']=$_REQUEST['json'];
    $data['cookie']=$_REQUEST['cookie'];
    $data['header']=$_REQUEST['header'];
    $data['likere']=$_REQUEST['likere'];
    $data['element']=$_REQUEST['element'];
    $data['method']=$_REQUEST['method'];
    $data['name']=$_REQUEST['name'];
    if(trim($_REQUEST['filters'])!="") {
        $data['filters'] = u($_REQUEST['filters']);
    }else{
        unset($data['filters']);
    }
    $data['gbk']=u($_REQUEST['gbk']);
    $data['data']=$_REQUEST['data'];
    $data['data2']=$_REQUEST['data2'];
    $data['data3']=$_REQUEST['data3'];//过滤结果
    $data['data4']=$_REQUEST['data4'];
    $data['tojsdata']=$_REQUEST['tojsdata'];
    $data['finally_prefix']=$_REQUEST['finally_prefix'];
    $id=insertupdate("json_host",$data,"select id from json_host where host='{$data['host']}' and likere='{$arr['likere']}'");
    return array($id,$data);
}

function json2Table($data,$tablename,$dbname,$wheresql,$keyname="id"){
    $da=array();
    if(is_array($data)&&is_array($data[0])){
        for($i=0;$i<count($data);$i++){
            json2Table($data[$i],$tablename,$dbname,$wheresql,$keyname="id");
        }
    }else{
        if(!empty($data)) {
            foreach ($data as $k => $v) {
                $f = getrow("SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE table_name = '{$tablename}' AND column_name = '{$k}'");
                if ($f != null) {
                    $da[$k] = $v;
                }
            }
        }
    }
    if(count($da)>0) {
        $da['update_time'] = time();
        if($da['create_time']==""){
            $da['create_time']=$da['update_time'];
        }elseif(preg_match("/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/",$da['create_time'])){
            $da['create_time']=strtotime( $da['create_time']);
        }
        $da['status'] = 1;
        $data = $da;
        $data['id']=SnowFlake::generateParticle();
        eval("\$wheresql=\"$wheresql\";");
        insertupdate("{$dbname}.{$tablename}", $data, $wheresql, $keyname, false);
    }
}

function savefilter($id){

    if(trim($_REQUEST['prodata'])!="") {
        $data=array();
        $data['filters']=$_REQUEST['prodata'];
        $data['postdata']=$_REQUEST['postdata'];
        $data['header']=$_REQUEST['header'];
        insertupdate("longzs.json_host", $data, "select id from longzs.json_host where id='{$id}'");
    }
    if(trim($_REQUEST['prowebdata'])!="") {
        $host=host($_REQUEST['url']);
        $data=array();
        $data['host']=$host;
        $data['prowebdata']=$_REQUEST['prowebdata'];
        insertupdate("longzs.longzs_re", $data, "select id from longzs.longzs_re where host='{$data['host']}'");
    }
}

function getUrlNames($result=array()){
    $r=query("select id,url,`name` from json_host group by `name`");
    $s="<option value='0'>请选择</option>";
    for($i=0;$i<count($r);$i++){
        $selected=($r[$i]['name']==$result['name'])?" selected":"";
        $s.="<option value='{$r[$i]['url']}'{$selected}>{$r[$i]['name']}</option>\r\n";
    }
    return $s;
}

function selecturl($host,$url){
    $result=array("host"=>$host);
    $arr=query("select * from json_host where host='{$host}'");
    for($i=0;$i<count($arr);$i++) {
        if (preg_match("/" . $arr[$i]['likere'] . "/Uisx", $url)) {
            if (trim($arr[$i]['include_php']) != "") {
                $toid = 0;
                $cookies = array();
                $args = parse_str2($_REQUEST["url"]);
                file_put_contents("temp.php", $arr[$i]['include_php']);
                include("temp.php");
                if ($toid > 0) {
                    $arr2 = query("select * from json_host where host='{$host}' and id={$toid}");
                    $arr2[0]['cookie'] = changeArgs($arr2[0]['cookie'], $cookies);
                    $arr2[0]['postdata'] = changeArgs($arr2[0]['postdata'], array_merge($args, $cookies));
                    $arr[$i] = $arr2[0];
                }
                $result=array_merge($result, $arr[$i]);
            }else{
                $result=$arr[$i];
            }
            break;
        }
    }
    $result['urlsnames']=getUrlNames($result);
    return $result;
}

function curl_get_cookies($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
    $result=curl_exec($ch);
    curl_close($ch);
    $cookies=parse_str2(getCookies($result));
    return $cookies;

}

function changeArgs($template,$args){
    foreach($args as $key=>$v){
        $template=preg_replace("/({$key}\s*)\=(\s*[^\;&\"\'\s]+)/i","\\1=".$v,$template);
        $template=preg_replace("/\"({$key})\"\s*\:\s*\"([^\;&\"\'\s]+)\"/i","\"\\1\":\"".$v."\"",$template);
        $template=preg_replace("/%22({$key})%22\+*%3A\+*([^\;&\%\'\+]+|[0-9]+|null)/i","%22\\1%22%3A%22".$v."%22",$template);
        $template=preg_replace("/%22({$key})%22\+*%3A\+*%22([^\;\%&\"\'\+]+)%22/i","%22\\1%22%3A%22".$v."%22",$template);

    }
    return $template;
}

function curl_post_cookies($url,$data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result=curl_exec($ch);
    curl_close($ch);
    $cookies=parse_str2(getCookies($result));
    return $cookies;
}

function parse_str2($str){
    preg_match_all("/([_\-a-z0-9]+)\s*\=\s*([^\s\;\&]+)/isx",$str,$nn);
    $arr=array();
    for($i=0;$i<count($nn[1]);$i++){
        $arr[$nn[1][$i]]=$nn[2][$i];
    }
    return $arr;
}



function fx(){
    global $data0,$smdata,$arr,$qian,$newarray,$filters,$searray,$qian2,$filterLine,$temparr;
    if(empty($data0)){
        echo "获取不到数据！";
        exit;
    }
    $data=print_r(bl0($data0),1);
    $databl=bl($data0);//代号化数组
    $data2=print_r($databl,1);
    $newarray=array();
    $smdata=array();
    $searray=array();
    createCode($databl);
    $data3=print_r($newarray,1);
    $data4=eval(" return \$newarray".$arr["finally_prefix"].";");
    if($data4==null){
        $data4=$smdata;
    }

    $data4=cldata4fun($data4);
    return [$data,$data2,$data3,$data4];
    if($arr["finally_prefix"]!="") {
        $data4 = eval(" return \$newarray" . $arr["finally_prefix"] . ";");
    }
    if($data4==""){
        $resultarray=array();
        $data4 = findkeys($newarray,"",$resultarray);
        $data4=$resultarray;
    }
    $resultData=$data4;
    if(!isset($_REQUEST['getPhpCode'])) $data4=print_r($data4,1);
    return [$data,$data2,$data3,$data4,$resultData];
}

function cldata4fun($data){
    $newarr=array();
    if(is_array($data)&&count($data)==1){
        foreach ($data as $key=>$value){
            $newarr[$key]=$value;
        }
    }
    if(is_array($data)&&!empty($data)) {
        foreach ($data as $k => $v) {
            if (preg_match("/\|(.*)$/U", $k, $nn)) {
                $k = preg_replace("/\|.*$/", "", $k);
                if (is_array($v)) {
                    $s = "";
                    for ($i = 0; $i < count($v); $i++) {
                        if (strlen($nn[1]) == 1) {
                            $s .= $v[$i] . $nn[1];
                        } elseif (strlen($nn[1]) == 0) {
                            $s .= $v[$i] . ",";
                        } else {
                            for ($j = 0; $j < count($v[$i]); $j++) {
                                $re = join("|", array_keys($v[$i]));
                                $s .= preg_replace_callback("/({$re})/", function ($em) use ($v, $i, $nn) {
                                        $em[1] = $v[$i][$em[1]];
                                        return $em[1];
                                    }, $nn[1]) . ",";
                            }
                        }
                    }
                    $s = substr($s, 0, -1);
                } else {
                    $s = $v;
                }
            } else {
                $s = $v;
            }
            $newarr[$k] = $s;
        }
    }else{
        $newarr=$data;
    }
    return $newarr;
}

function findkeys($arr,$pre,&$resultarray){
    global $filters;
    $filters=preg_replace("/\[\"[^\n\[\]]*\"\]\:([^\,\n]+)/mi","[\"\\1\"]:\\1",$filters);
    foreach($arr as $k=>$v){
        $pre2="";
        $k2=is_int($k)?0:$k;
        if($k2===0){
            $pre_re=$pre."[$k2]";
            $pre2=$pre."[$k]";
        }else{
            $pre_re=$pre."[\"{$k}\"]";
            $pre2=$pre."[\"{$k}\"]";
        }
        $pre_re=preg_replace("/\[[0-9]+\\\?\]/","[0]",$pre_re);
        if(preg_match("/".addsla($pre_re)."\:([^\,\n]+)/isx",$filters,$nn)){
            $resultarray[$nn[1]]=$v;
        }
        if(is_array($v)) {
            $arr[$k] = findkeys($v, $pre2,$resultarray);
        }else if(is_string($v)||is_integer($v)||is_float($v)||$v==true||$v==false){
            if($v!="") $arr[$k]=$v;
        }
    }
    return $arr;
}

function getDataFromHttpHeader(){
    global $host,$searray,$newarray,$qian,$qian2,$smdata,$filters,$arr,$data0,$_REQUEST,$result,$longthis;
    if($longthis->yuannr){
        $html=$longthis->yuannr;
        $result['tojsdata']=$longthis->rs['re'];
    }elseif($_REQUEST['getPhpCode']==""&&($_REQUEST['url']!=""&&$_REQUEST['element']===null||$_REQUEST['element']=="")) {
        $_REQUEST['header']=$_REQUEST['header']==""?array("Content-Type: application/x-www-form-urlencoded"):$_REQUEST['header'];
        $html = curl_file_get_contents_api($_REQUEST['url'],$_REQUEST['method'],$_REQUEST['data'],0,$_REQUEST['cookie'],$_REQUEST['header']);
    }elseif($_REQUEST['httpheader']!=""){
        preg_match("/^(post|get)\s+([^\r\n]*)\s+http[^\r\n]*?\r??\n??(.*)(?:\r\n|\n\r|\n){2,}(.*)$/Uis",$_REQUEST['httpheader'],$nn);
        $method=$nn[1];
        $url=$nn[2];
        $data=trim($nn[4]);
        $header=trim(preg_replace("/^Cookie\:.*$/im","",$nn[3]));
        preg_match("/^Cookie\:\s*([^\r\n]+)/im",$_REQUEST['httpheader'],$cookienn);
        $cookie=trim($cookienn[1]);
        $html = curl_file_get_contents_api($url,$method,$data,0,$cookie,$header);
    }else {
        $html = element2($_REQUEST['url'], $_REQUEST['element'], 10000);
    }
    $html=trim($html);
    $html=u(trim($html));
//    if(preg_match("/^\s*(<html|<\!DOCTYPE\s+html)/Uisx",$html)&&trim($result['tojsdata'])!=""){
//        preg_match_all("/^([^\r\n]+)\:tojsdata/Uimx",$result['tojsdata'],$mm);
//        for($i=0;$i<count($mm[1]);$i++){
//            if(preg_match("/".$mm[1][$i]."/Uisx",$html,$mm2)){
//                $html=$mm2[1];
//                break;
//            }else{
//                continue;
//            }
//        }
//    }

    $resultdata=array();
    if(trim($_REQUEST['tojsdata'])!=""){
        preg_match("/".$_REQUEST['tojsdata']."/Uisx",$html,$nn);
        $jsonhtml=$nn[1];
        $data0=nodecode($jsonhtml,1);
        if($longthis) {
            $arr = getJsonHost($longthis->url);
        }
        $filters=$arr['filters'];
        list($data,$data2,$data3,$data4,$resultdata)=fx();
        $resultdata=$data4;
    }elseif($result['tojsdata']!=""){
        preg_match_all("/^(.*)\:tojsdata/Uim",$result['tojsdata'],$nn2);
        if(!empty($nn2[1])) {
            for ($i = 0; $i < count($nn2[1]); $i++) {
                preg_match("/{$nn2[1][$i]}/Uisx", $html, $nn3);
                if ($nn3[1] != "") {
                    $jsonhtml = $nn3[1];
                    $result['tojsdata'] = $nn2[1][$i];
                    $data0=nodecode($jsonhtml,1);
                    if($longthis->url) {
                        $arr = getJsonHost($longthis->url);
                        $filters=$arr['filters'];
                    }
                    list($data,$data2,$data3,$data4,$resultdata)=fx();
                    $resultdata=($resultdata==null)?array():$resultdata;
                    $resultdata=$resultdata+$data4;
                }
            }
        }
    }
    if(!isset($_REQUEST['getPhpCode'])) $data4=print_r($data4,1);
    return [$data,$data2,$data3,$data4,$data0,$resultdata];
}

function clmb($content,$argv){
    foreach($argv as $k=>$v){
        $content=preg_replace("/\{{$k}\}/",$v,$content);
    }
    //$content=preg_replace("/^Cookie\:.*$/mi","",$content);
    $content=trim($content);
    return $content;
}

function getPhpCode(){
    $_REQUEST['header']=trim($_REQUEST['header']);
    $method=$_REQUEST['method'];
    //$_REQUEST['header']=str_replace('*/*','*/ *',$_REQUEST['header']);
    //$_REQUEST['header']=preg_replace('/\nContent\-Type\:[^\r\n]+/imx',"\nContent-Type: application/x-www-form-urlencoded",$_REQUEST['header']);
    $_REQUEST['cookie']=trim($_REQUEST['cookie']);
    $httpheader="{$method} {$_REQUEST['url']} HTTP/1.1
{$_REQUEST['header']}
Cookie: {$_REQUEST['cookie']}     

{$_REQUEST['data']}

";
    $argv['httpheader'] = $httpheader;
    $content=getone("select content from `longzs_mb` where name='phpcode'");
    $content=clmb($content,$argv);
    return $content;
}

function getJsonHost($url){
    global $isfinded;
    if(preg_match("/^(post|get)\s+([^\r\n]*)\s+http[^\r\n]*?\r??\n??(.*)(?:\r\n|\n\r|\n){2,}(.*)$/Uis",$url,$nn)) {
        $url = trim($nn[2]);
        $host = host($nn[2]);
    }else{
        $host = host($url);
    }
    $result=getCurrJsonHost($host,$url);
    return $result;
}

function getCurrJsonHost($host,$url){
    $result=array();
    $arr=query("select * from json_host where host='{$host}'");
    $isfinded=false;
    for($i=0;$i<count($arr);$i++){
        if(preg_match("/".$arr[$i]['likere']."/Uisx",$url)){
            $result=$arr[$i];
            $isfinded=true;
            break;
        }
    }
    return $result;
}

function bugv() {
    static $static;
    global $smdata;
    if ($smdata!=$static) {
        error_log("Value changed. Stopping for debug.");
    }
    $static = $smdata;
}

function addsla4($str){
    $re = preg_replace("/([\[\]\{\}])/", "\\\\\\1", $str);
    $re = preg_replace("/\\\[[0-9]+\\\]/", "\\\[[0-9]+\\\]", $re);
    $re = preg_replace("/^\\\?\[\"\"\\\?\]/", "", $re);
    $re = preg_replace("/\"/", "\\\"", $re);
    $re = str_replace('/', '\/', $re);
    return $re;
}

function isNumericKeyArray($value) {
    return is_array($value) && !array_filter(array_keys($value), 'is_string');
}

function mustInArr($r){
    $result=array();
    foreach ($r as $k=>$v){
        $nn1=explode("|",$v['nameStr']);
        for ($i = 0; $i < count($nn1); $i++) {
            $nn2=explode(">",$nn1[$i]);
            $result[]=end($nn2);
        }
    }
    return $result;
}

function prints($s){
    $nn=explode(",",$s);
    $result="";
    for ($i = 0; $i < count($nn); $i++) {
        $result.="\$".$nn[$i]."=".preg_replace("/\s+/","",print_r($GLOBALS[$nn[$i]],1)).";";
    }
    $result=preg_replace_callback("/Array\(((?:\[[0-9]+\]=>([^\[\]]+))+)\)/Uisx",function($em){
        $str=$em[1];
        preg_match_all("/\[[0-9]+\]=>([^\[\]]+)/isx",$str,$nn);
        $result=array();
        for ($i = 0; $i < count($nn); $i++) {
            if($nn[1][$i]) $result[]=$nn[1][$i];
        }
        return "[".join(",",$result)."]";
    },$result);
    return $result;
}

//=== --yzs 连接字符join
function joinStr($value,$prenn){
    global $arrSelectKeys;
    if(preg_match("/join=\"(.*)\"/Uisx",$prenn[0],$nn33)){
        $nn44=preg_split("/,/",$nn33[1],2);
        $join=$nn44[0];
        $join2=$nn44[1];
        if($nn44[1]==""){
            $join2=$join?$join:(($join2=="")?$nn33[1]:$join2);
            $join="";
        };
    }else{
        unset($join);
        return $value;
    }

    $joinStr="";
    $inarr = explode(",", $arrSelectKeys);
    foreach ($value as $k => $v) {
        $arr3=array();
        foreach ($v as $k2 => $v2) {
            if (in_array($k2, $inarr)) {
                $v2=preg_replace("/&gt;/","",$v2);
                $arr3[$k2]=$v2;
                $joinStr.=$v2.$join;
            }
        }
        $joinStr=substr_replace($joinStr, '', !strripos($joinStr,$join)?0:strripos($joinStr,$join), strlen($join)).$join2;
    }
    $joinStr=substr_replace($joinStr, '', strripos($joinStr,$join2), strlen($join2));
    return $joinStr;
}